﻿using System;
using System.Drawing;
using System.Windows.Forms;

namespace StockLib.Indicator
{
    public partial class StockKdjIndicator : StockIndicator
    {
        public override void DrawGraphTitle(Graphics g, Rectangle rect)
        {
            int y = (rect.Top + rect.Bottom) / 2;
            int x = rect.Left;
            string textOutput = string.Format("MACD({0},{1},{2})", rsvDays, kDays, dDays);
            TextRenderer.DrawText(g, textOutput, font, new Point(x, y), StockProfile.Colors.GraphTextColor, TextFormatFlags.Left | TextFormatFlags.VerticalCenter);
            x += TextRenderer.MeasureText(textOutput, font).Width;

            float[] outValues = CalcOutput(indexEnd);
            for (int i = 0; i < 3; i++)
            {
                switch (i)
                {
                    case 0:
                        textOutput = string.Format("DIF:{0:#0.00}", outValues[i]);
                        break;

                    case 1:
                        textOutput = string.Format("DEA:{0:#0.00}", outValues[i]);
                        break;

                    case 2:
                        textOutput = string.Format("MACD:{0:#0.00}", outValues[i]);
                        break;
                }


                Color color = StockProfile.IndicatorColors[i];
                TextRenderer.DrawText(g, textOutput, font, new Point(x, y), color, TextFormatFlags.Left | TextFormatFlags.VerticalCenter);
                x += TextRenderer.MeasureText(textOutput, font).Width;
            }
        }

        public override void DrawGraphData(Graphics g)
        {
            int yPosZero = GetYPos(0);

            // 绘制输出值
            for (int index = indexStart; index <= indexEnd; index++)
            {                
                if (index > 1)
                {
                    float[] indicatorValues = CalcOutput(index);
                    float[] indicatorValuesLast = CalcOutput(index - 1);

                    int xPos = GetXPos(index);
                    int xPosLast = GetXPos(index - 1);
                    for (int i = 0; i < cacheCount; i++)
                    {
                        int yPos = GetYPos(indicatorValues[i]);
                        int yPosLast = GetYPos(indicatorValuesLast[i]);
                        switch (i)
                        {
                            case 0:
                            case 1:
                                g.DrawLine(StockProfile.IndicatorPens[i], xPosLast, yPosLast, xPos, yPos);
                                break;

                            case 2:                                
                                Pen pen = (indicatorValues[i] > 0) ? StockProfile.Pens.UpLinePen : StockProfile.Pens.DnLinePen;
                                g.DrawLine(pen, xPos, yPos, xPos, yPosZero);
                                break;
                        }
                    }
                }
            }


            // 绘制0轴
            using (Pen pen = new Pen(StockProfile.Colors.GraphTextColor))
            {
                pen.DashPattern = new float[] { 1, 1 };
                pen.DashStyle = System.Drawing.Drawing2D.DashStyle.Custom;
                g.DrawLine(pen, rectGraph.Left, yPosZero, rectGraph.Right, yPosZero);
            }
        }

        protected override void PrepareDrawSetting(Rectangle rectAll)
        {
            base.PrepareDrawSetting(rectAll);

            graphMargin.Top = graphMargin.Bottom = 5;
        }
    }
}
